//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Galleries.Components;

namespace CommunityServer.Galleries.Controls
{
	/// <summary>
	/// Summary description for AggregateUpdatedGalleriesListing.
	/// </summary>
	public class AggregateUpdatedGalleriesListing : GalleryTemplatedWebControl
	{

		#region Child Controls

		private Repeater repeater;

		#endregion

		#region Public Properties

		private int galleryCount = 10;
		public int GalleryCount
		{
			get { return galleryCount; }
			set { galleryCount = value; }
		}

		private int pictureCount = 5;
		public int PictureCount
		{
			get { return pictureCount; }
			set { pictureCount = value; }
		}

		#endregion

		#region Skin

		protected override void AttachChildControls()
		{
			repeater = FindControl( "GalleryRepeater" ) as Repeater;
			repeater.ItemDataBound += new RepeaterItemEventHandler(galleries_ItemDataBound);
		}

		#endregion

		#region Overrides

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			if(!Page.IsPostBack)
				DataBind();
		}

		public override void DataBind()
		{
			base.DataBind();

			ArrayList allGalleries = Galleries.GetGalleries();
			
			ArrayList galleries = new ArrayList();
			foreach(Gallery g in allGalleries)
				if(g.IsActive)
					galleries.Add(g);
 
			galleries.Sort(new SectionSorter(SectionSortBy.LastPost, SortOrder.Descending));

			if(galleries.Count < galleryCount)
				galleryCount = galleries.Count;

			Gallery[] pageItems = new Gallery[galleryCount];
			galleries.CopyTo(0, pageItems, 0, galleryCount);

			repeater.DataSource = pageItems;
			repeater.DataBind();
		}

		#endregion

		#region Events

		private void galleries_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			Gallery gallery = e.Item.DataItem as Gallery;

			switch(e.Item.ItemType)
			{
				case ListItemType.AlternatingItem:
				case ListItemType.Item:

					Repeater pictureRepeater = e.Item.FindControl("PictureRepeater") as Repeater;
					pictureRepeater.ItemDataBound += new RepeaterItemEventHandler(pictureRepeater_ItemDataBound);

					GalleryThreadQuery query = new GalleryThreadQuery();
					query.SectionID = gallery.SectionID;
					query.PageSize = PictureCount;
					query.SortBy = GalleryThreadSortBy.ThreadDate;
					query.SortOrder = SortOrder.Descending;
					query.ApplicationPostType = GalleryPostType.Image;
					query.PublishedFilter = GalleryPostPublishedFilter.Published;

					HyperLink galleryLink = e.Item.FindControl("GalleryLink") as HyperLink;
					if(galleryLink != null)
					{
						galleryLink.Text = gallery.Name;
						galleryLink.NavigateUrl = GalleryUrls.Instance().ViewGallery(gallery.ApplicationKey);
					}

					if(gallery.TotalThreads > 0)
					{
						pictureRepeater.DataSource = GalleryPosts.GetPictures(query, true, true).Threads;
						pictureRepeater.DataBind();

						Literal updated = e.Item.FindControl("Updated") as Literal;
						if(updated != null)
							updated.Text = Formatter.FormatAgoDate(gallery.MostRecentPostDate);//gallery.MostRecentPostDate.ToString(CSContext.Current.SiteSettings.DateFormat + " " + CSContext.Current.SiteSettings.TimeFormat);

						HyperLink userLink = e.Item.FindControl("UserLink") as HyperLink;
						if(userLink != null)
						{
							userLink.Text = gallery.MostRecentPostAuthor;
							userLink.NavigateUrl = Globals.GetSiteUrls().UserProfile(gallery.MostRecentPostAuthorID);
						}
					}
					else
					{
						Control updatedFooter = e.Item.FindControl("UpdatedFooter");
						if(updatedFooter != null)
							updatedFooter.Visible = false;
					}
					break;
			}
		}

		private void pictureRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			GalleryPost galleryPost = e.Item.DataItem as GalleryPost;

			try
			{
				switch(e.Item.ItemType)
				{
					case ListItemType.AlternatingItem:
					case ListItemType.Item:

						GalleryImage image = e.Item.FindControl("GalleryImage") as GalleryImage;
						if(image != null)
						{
							image.NavigateUrl = GalleryUrls.Instance().ViewPicture( Galleries.GetGallery(galleryPost.SectionID).ApplicationKey, galleryPost );
							image.GalleryPost = galleryPost;
						}

						break;
				}
			}
			catch{}
		}

		#endregion

	}
}
